#include <stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <linux/if.h>
//#include <linux/in.h>
#include <string.h>
#include <linux/sockios.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>


#define IP_SIZE 128

#define PROCBUFSIZ                  1024
#define _PATH_PROC_NET_DEV        "/proc/net/dev"
static char * interface_name_cut (char *buf, char **name)
{
  char *stat;
  /* Skip white space.  Line will include header spaces. */
  while (*buf == ' ')
    buf++;
  *name = buf;
  /* Cut interface name. */
  stat = strrchr (buf, ':');
  *stat++ = '\0';
  return stat;
}

int list_interface_valid()
{
  FILE *fp;
  char buf[PROCBUFSIZ];
  char *name;
  char *p;
 
  /* Open /proc/net/dev. */
  fp = fopen (_PATH_PROC_NET_DEV, "r");
  if (fp == NULL)
    {   
        printf("open proc file error\n");
      return -1; 
    }   
 
  /* Drop header lines. */
  fgets (buf, PROCBUFSIZ, fp);
  fgets (buf, PROCBUFSIZ, fp);
 
  /* Only allocate interface structure.  Other jobs will be done in
     if_ioctl.c. */
  while (fgets (buf, PROCBUFSIZ, fp) != NULL)
    {   
      p = interface_name_cut (buf, &name);
	  printf("port=%s\n",name);
    }   
  fclose(fp);
  return 0;
}

int check_interface_fromproc(char *interface)
{
  FILE *fp;
  char buf[PROCBUFSIZ];
  struct interface *ifp;
  char *name;
  char *p;
 
  /* Open /proc/net/dev. */
  fp = fopen (_PATH_PROC_NET_DEV, "r");
  if (fp == NULL)
    {   
        printf("open proc file error\n");
      return -1; 
    }   
 
  /* Drop header lines. */
  fgets (buf, PROCBUFSIZ, fp);
  fgets (buf, PROCBUFSIZ, fp);
 
  /* Only allocate interface structure.  Other jobs will be done in
     if_ioctl.c. */
  while (fgets (buf, PROCBUFSIZ, fp) != NULL)
    {   
      p = interface_name_cut (buf, &name);
      if(strcmp(interface,name)==0)
          return 1;
    }   
  fclose(fp);
  return 0;
}
/**
 * 获取本地IP
 * @param eth
 * @param ip
 * @return
 */
int getLocalIp(const char *eth, char *ip) {
    struct ifreq ifr;
    struct sockaddr_in sin;
    int fd;
    bzero(&ifr, sizeof(ifr));
    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        return -1;
    }
    strcpy(ifr.ifr_name, eth);
    if (ioctl(fd, SIOCGIFADDR, &ifr) < 0) {
        close(fd);
        return -1;
    }
    memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
    snprintf(ip, IP_SIZE, "%s", inet_ntoa(sin.sin_addr));
    close(fd);
    return 0;
}
/**
 * 获取本地Mac地址
 * @param eth
 * @param mac
 * @return
 */
int getLocalMac(const char *eth, char *mac) {


    int fd;
    struct ifreq ifr;
    bzero(&ifr, sizeof(ifr));
    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        return -1;
    }
    strcpy(ifr.ifr_name, eth);
    if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0)
    {
        close(fd);
        return -1;
    }
   snprintf(mac,18, "%02x:%02x:%02x:%02x:%02x:%02x",
         (unsigned char) ifr.ifr_hwaddr.sa_data[0],
         (unsigned char) ifr.ifr_hwaddr.sa_data[1],
         (unsigned char) ifr.ifr_hwaddr.sa_data[2],
         (unsigned char) ifr.ifr_hwaddr.sa_data[3],
         (unsigned char) ifr.ifr_hwaddr.sa_data[4],
         (unsigned char) ifr.ifr_hwaddr.sa_data[5]);
    close(fd);
    return 0;

} 

int main()
{
	int fg=0;
	char mac[32]={};
	char ip[IP_SIZE]={0};


	list_interface_valid();

	fg=check_interface_fromproc("eth0");	
#if 1
	if(fg == 1)
	{
		printf("eth0 up\n");
	}else{
		printf("eth0 down\n");
	}
	fg=check_interface_fromproc("eth1");
	if(fg == 1)
	{
		printf("eth1 up\n");
	}else{
		printf("eth1 down\n");
	}
	fg=check_interface_fromproc("lo");
	if(fg == 1)
	{
		printf("lo up\n");
	}else{
		printf("lo down\n");
	}		
	getLocalIp("lo",ip);
	printf("lo:ip=%s\n",ip);
	
	getLocalIp("eth0",ip);
	printf("eth0:eth0=%s\n",ip);
	
	getLocalMac("eth0",mac);
	printf("eth0 mac:%s\n",mac);
#endif
	return 1;
}